﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<UI.Areas.Admin.Models.ProductModel>" %>
<%@ Import Namespace="UI.Helper" %>

<%@ Register assembly="CKEditor.NET" namespace="CKEditor.NET" tagprefix="CKEditor" %>

<%= Utils.IncludeJS(this.Html, "~/Scripts/jquery.validate.min.js")%>
<%= Utils.IncludeJS(this.Html, "~/Scripts/Admin/ckeditor/ckeditor.js")%>
<%= Html.IncludeJS("~/Scripts/Admin/ckeditor/adapters/jquery.js") %>
<!-- start id-form -->
<table border="0" cellpadding="0" cellspacing="0" id="id-form">
    <tr>
        <th valign="top">
            Tên:
        </th>
        <td>
            <%: Html.TextBoxFor(c => c.Name, new { @class = "inp-form", id = "Name" })%>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <th valign="top">
            Địa chỉ:
        </th>
        <td>
            <%: Html.TextBoxFor(c => c.Address, new { @class = "inp-form", id = "Address" })%>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <th valign="top">
            Danh mục:
        </th>
        <td>
            <% if (Model.CategoryId > 0)
               {%>
                   <%: Html.DropDownListFor(c => c.CategoryId, new SelectList(Model.Categories, "Id", "Name", Model.CategoryId))%>
               <%} %>
            <% else
                   {%>
                       <%: Html.DropDownListFor(c => c.CategoryId, new SelectList(Model.Categories, "Id", "Name"))%>
                   <%} %>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <th valign="top">
            Tỉnh/thành:
        </th>
        <td>
            <% if (Model.ProvinceId > 0)
               {%>
                   <%: Html.DropDownListFor(c => c.ProvinceId, new SelectList(Model.Provinces, "Id", "Name", Model.ProvinceId))%>
               <%} %>
            <% else
                   {%>
                       <%: Html.DropDownListFor(c => c.ProvinceId, new SelectList(Model.Provinces, "Id", "Name"))%>
                   <%} %>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <th valign="top">
            Quận/huyện:
        </th>
        <td>
            <% if (Model.DistrictId > 0)
               {%>
                   <%: Html.DropDownListFor(c => c.DistrictId, new SelectList(Model.Districts, "Id", "Name", Model.DistrictId))%>
               <%} %>
            <% else
                   {%>
                       <%: Html.DropDownListFor(c => c.DistrictId, new SelectList(Model.Districts, "Id", "Name"))%>
                   <%} %>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <th valign="top">
            Mô tả:
        </th>
        <td>
            
            <%: Html.TextAreaFor(c => c.Description, new { id = "Description" })%>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <th valign="top">
            Là sản phẩm Hot:
        </th>
        <td>
            <%: Html.CheckBoxFor(c => c.IsHot, new { @class = "inp-form", id = "IsHot" })%>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <th valign="top">
            Độ ưu tiên:
        </th>
        <td>
            <%: Html.TextBoxFor(c => c.Priority, new { @class = "inp-form", id = "Priority" })%>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <th>
            &nbsp;
        </th>
        <td valign="top">
            <input type="submit" value="" class="form-submit" />
            <% if (Model.ProductId > 0)
               {%>
                <a id="linkToImage" href="<%: Url.Content("~/Admin/Image/Index/") + Model.ProductId %>">
                <%--<%: Html.ActionLink("Hình ảnh", "Index", "Image", new { id = Model.ProductId }, null) %>--%>
                <img src="<%: Url.Content("~/Images/admin/forms/image_btn.png")%>"/>
                </a>
                <%: Html.HiddenFor(o => o.ProductId) %>
            <%
               }%>
        </td>
        <td>
        </td>
    </tr>
</table>
<!-- end id-form  -->

<script type="text/javascript">
    $(document).ready(function () {
        $("#infoForm").validate({
            rules:
            {
                Name: { required: true },
                Address: { required: true },
                Description: { required: true },
                Priority: { required: true, digits: true }
            },
            messages:
            {
                Name: { required: 'Hãy nhập tên bất động sản.' },
                Address: { required: 'Hãy nhập địa chỉ bất động sản.' },
                Description: { required: 'Hãy nhập mô tả bất động sản.' },
                Priority: { required: 'Độ ưu tiên phải là số.' }
            },
            errorElement: "div"
        });

        $('#ProvinceId').change(function () {
            $.getJSON('<%: Url.Content("~/Admin/Product/GetDistrictByProvince/") %>' + this.value, function (data) {
                var dropdownList = document.getElementById("DistrictId");
                dropdownList.options.length = 0;
                $.each(data, function (index, optionData) {
                    
                    var option = new Option(optionData.Text, optionData.Value);

                    if ($.browser.msie) {
                        dropdownList.add(option);
                    }
                    else {
                        dropdownList.add(option, null);
                    }
                });

            });

        });

        //CKEditor
        $('#Description').ckeditor();
        var editor = CKEDITOR.instances.Description;

        editor.on("instanceReady", function () {
            //set keyup event
            this.document.on("keyup", ChangeDescription);

            //and paste event
            this.document.on("paste", ChangeDescription);
        })

        function ChangeDescription() {
            var data = editor.getData();
            $("#Description").attr("value", data);
            //CKEDITOR.instances.editor1.updateElement(); 
        }
    });
</script>